import axios from 'axios';
import store from "../store"
import qs from 'qs';
// import {
//     message
// } from 'ant-design-vue';
import config from '../api/config.js'
import {
  getToken
} from "../util/validate.js"

// 请求超时时间
axios.defaults.timeout = 30000;
axios.defaults.withCredentials = false
// post请求头
// axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded;charset=UTF-8';
// axios.defaults.headers.post['Content-Type'] = 'application/json;charset=UTF-8';
axios.defaults.headers.post['X-Requested-With'] = 'XMLHttpRequest'
axios.defaults.headers.get['X-Requested-With'] = 'XMLHttpRequest'

// 请求拦截器
axios.interceptors.request.use(
  config => {
    let token = getToken() || null
    if (token) {
      config.headers['X-Access-Token'] = token
      config.headers['realServerName'] = 'www.51bmj.cn'
    }
    return config;
  },
  error => {
    // message.error({
    //     message: "网络异常"
    // })
  })

// 响应拦截器
axios.interceptors.response.use(
  response => {
    if (response.status === 200) {
      if (response.data.code == "4010" || response.data.code == "30001") {
        window.localStorage.removeItem("saasToken")
        window.localStorage.removeItem("userInfo")
        location.href = config.loginRedirectUrl;
      }
      return Promise.resolve(response);
    } else {
      if (response.message = "X-Access-Token不能为空") {
        this.$message({
          type: 'warning',
          message: '登录超时,请重新登录'
        })
        this.$store.dispatch('increment')
        // store.dispatch('handleLogout')
        setTimeout(() => {
          this.$router.push("/login")
        }, 2000)


      } else {
        return Promise.reject(response);
      }

    }

  },
  // 服务器状态码不是200的情况
  // error => {
  //   if (error.response.status === 509) {
  //     let html = error.response.data;
  //     let verifyWindow = window.open("", "_self", "height=400,width=560");
  //     verifyWindow.document.write(html);
  //     verifyWindow.document.getElementById("baseUrl").value = baseUrl;
  //   } else {
  //     return Promise.reject(error.response);
  //   }
  // }
);

/**
 * get方法，对应get请求
 * @param {String} url [请求的url地址]
 * @param {Object} params [请求时携带的参数]
 */
export function get(url, params, responseType) {
  // console.log('get',url)
  let data = Object.assign({}, params)
  return new Promise((resolve, reject) => {
    axios.get(url, {
        params: data,
        responseType
      }, )
      .then(res => {
        if (res.data.code == 200) {
          resolve(res.data.result)
        } else {
          resolve(res.data)
          // message.error({
          //     message: res.data.message
          // })
        }
      })
      .catch(err => {
        reject(err)
      })
  });
}

/**
 * get方法，对应get请求
 * @param {String} url [请求的url地址]
 * @param {Object} params [请求时携带的参数]
 */
export function getFile(url, params) {
  let data = Object.assign({}, params)
  return new Promise((resolve, reject) => {
    axios.get(url, {
        params: data,
        responseType: 'blob'
      }, )
      .then(res => {
        if (res.data.code == 200) {
          resolve(res.data)
        } else {
          resolve(res)
        }
      })
      .catch(err => {
        reject(err)
      })
  });
}

/**
 * post方法，对应post请求
 * @param {String} url [请求的url地址]
 * @param {Object} params [请求时携带的参数]
 */
export function post(url, params) {
  // console.log('post',url)
  let data = Object.assign({}, params)
  return new Promise((resolve, reject) => {
    axios.post(url, data)
      .then(res => {
        if (res.data.status == "1" || res.data.httpCode == "200") {
          resolve(res.data)
        } else {
          resolve(res.data)
          // message.error({
          //     message: res.data.msg || res.data.message
          // })
        }
        //resolve(res.data);
      })
      .catch(err => {
        reject(err)
      })
  });
}

/**
 * get方法，对应get请求
 * @param {String} url [请求的url地址]
 * @param {Object} params [请求时携带的参数]
 */
export function get2(url, params, responseType) {
  // console.log('get2',url)
  let data = Object.assign({}, params)
  return new Promise((resolve, reject) => {
    axios.get(url, {
        params: data,
        responseType
      }, )
      .then(res => {
        // console.log('---------------1')
        if (res.data.code == 200) {
          resolve(res.data)
        } else {
          resolve(res.data)
          // message.error({
          //     message: res.data.message
          // })
        }
      })
      .catch(err => {
        // console.log('---------------2')
        reject(err)
      })
  });
}

/**
 * post方法，对应post请求
 * @param {String} url [请求的url地址]
 * @param {Object} params [请求时携带的参数]
 */
export function deleteFun(url, params) {
  let data = Object.assign({}, params)
  return new Promise((resolve, reject) => {
    axios.delete(url, data)
      .then(res => {
        if (res.data.code == 200) {
          resolve(res.data)
        } else {
          resolve(res.data)
        }
      })
      .catch(err => {
        reject(err)
      })
  });
}
